/**
 *	\file AKHumanPlayer.m
 *  
 *  \section desc_sec Description
 *  Description
 *  
 *  \if copyright
 *  	Copyright 2008 Ben Chen. All rights reserved.
 *  \endif
 *  
 *  \section hist_sec Change History:
 *  	-	2/3/08		BC		Created this file
 *  
 *  <hr>
 *  \addtogroup AnimalKingdom
 *  @{
 */

#import "AKHumanPlayer.h"
#import "AKStateMaintainer.h"

@interface AKHumanPlayer ()
@property (assign) AKMove		bestMove;
@end

@implementation AKHumanPlayer

@synthesize interactiveDrawing, stateMaintainer, bestMove, side;

#pragma mark Mouse Events

- (void)mouseMoved:(NSEvent *)theEvent
{
	NSLog(@"mouseMoved");
	
	NSPoint pointInView = [(interactiveDrawing.drawingView) convertPoint:[theEvent locationInWindow]
																fromView:nil];
	
	AKCellIndex cellIndex = [interactiveDrawing cellIndexForPoint:pointInView];
	if (cellIndex != NSNotFound)
	{
		NSLog(@"cellIndex:%d", cellIndex);
		AKPiece aPiece = [stateMaintainer moveablePieceAtCell:cellIndex];
		
		NSLog(@"piece:%@", AKStringFromPiece(aPiece));
		
		if (!AKEqualPieces(aPiece, AKZeroPiece) && aPiece.side == self.side)
			[interactiveDrawing activatePiece:aPiece];
		else
			[interactiveDrawing deactivatePiece];
	}
}

- (void)dragPiece//WithEvent:(NSEvent*)theEvent
{
	while (1) 
	{
		NSEvent *theEvent = [[(interactiveDrawing.drawingView) window] nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
		
		NSPoint pointInView = [(interactiveDrawing.drawingView) convertPoint:[theEvent locationInWindow]
																	fromView:nil]; 
		[interactiveDrawing dragPieceAt:pointInView];
		
		if ([theEvent type] == NSLeftMouseUp)
		{
			[interactiveDrawing stopDragging];//reachableCellForEndPoint:pointInView];
			
			/*
			AKMove newMove;
			newMove.piece	= draggingPiece.piece;
			newMove.side	= draggingPiece.side;
			newMove.srcCell	= draggingPiece.cell;
			newMove.dstCell	= endCell;
			
			self.bestMove = newMove;
			
			canResignFirstResponder = YES;*/
			
			break;
		}
	}
}

- (void)mouseDown:(NSEvent *)theEvent
{
	NSLog(@"mouseDown!");
		
	NSPoint pointInView = [(interactiveDrawing.drawingView) convertPoint:[theEvent locationInWindow]
																fromView:nil]; 
	AKCellIndex cellIndex = [interactiveDrawing cellIndexForPoint:pointInView];
	
	AKPiece moveablePiece = [stateMaintainer moveablePieceAtCell:cellIndex];
	
	if (AKEqualPieces(moveablePiece, AKZeroPiece)) {		
		NSArray *newPieces = [stateMaintainer newPiecesAtCell:cellIndex];
		
		if (newPieces) {
			AKMove newMove;
			newMove.piece	= [[newPieces objectAtIndex:0] unsignedIntValue];
			newMove.side	= self.side;
			newMove.srcCell	= NSNotFound;
			newMove.dstCell	= cellIndex;
			self.bestMove = newMove; // should trigger observer's method
		}
	}
	else {
		[interactiveDrawing aboutToDragPiece:moveablePiece atPoint:pointInView];
		[self dragPiece];
	}
	
	/*
	NSPoint pointInView = [(interactiveDrawing.drawingView) convertPoint:[theEvent locationInWindow]
																fromView:nil]; 
	
	draggingPiece = [interactiveDrawing pieceForPoint:pointInView];
	
	if (AKEqualPieces(draggingPiece, AKZeroPiece))
	{
		// If we got here then there must be nothing else to do.  Just track until mouseUp:.
		while (1) 
		{
			theEvent = [[(interactiveDrawing.drawingView) window] nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
			if ([theEvent type] == NSLeftMouseUp)
				break;
		}
	}
	else
	{
		[self dragPieceWithEvent:theEvent];
	}		
	 */
}

- (void)mouseUp:(NSEvent *)theEvent
{
}

#pragma mark AKPlayer methods

- (void)backToInitialState
{
}

- (BOOL)thinkNextMove
{
	//[[(interactiveDrawing.drawingView) window] makeFirstResponder:(interactiveDrawing.drawingView)];
	[(interactiveDrawing.drawingView) setNextResponder:self];
	
	return YES;
}

- (void)stopThinking
{
	[(interactiveDrawing.drawingView) setNextResponder:nil];
}

- (BOOL)performMove:(AKMove)newMove
{
	return YES;	// Since humen should "maintain" the state themselves
}

#pragma mark Plug-in

- (NSString*)gameName
{
	return AKPlugInAllGames;
}

- (NSString*)version
{
	return AKPlugInAllVersions;
}

@end

/** @} */
